package com.ctg.payroll2.service.social.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ctg.payroll2.bean.filter.social.PbSocialDetailFilter;
import com.ctg.payroll2.bean.filter.social.PbSocialMasterFilter;
import com.ctg.payroll2.bean.po.social.PbSocialDetail;
import com.ctg.payroll2.bean.po.social.PbSocialMaster;
import com.ctg.payroll2.bean.po.social.RecomTemplateSocialItemSearchBean;
import com.ctg.payroll2.service.BaseService;
import com.ctg.payroll2.service.social.PbSocialItemService;
import com.ctg.platform.orm.ibatis.IbatisEntityDao;
import com.ctg.platform.util.Page;
/**
 * 社保项目service实现
 * @author 鲁力 2009-9-7 下午06:39:11
 *
 */
@SuppressWarnings("unchecked")
public class PbSocialItemServiceImpl extends BaseService implements PbSocialItemService {
	private String appendString = "intersect ";
	private IbatisEntityDao ibatisEntityDao;
	/**
	 * @param ibatisEntityDao 设置ibatisEntityDao
	 */
	public void setIbatisEntityDao(IbatisEntityDao ibatisEntityDao) {
		this.ibatisEntityDao = ibatisEntityDao;
	}

	public List getPbSocialItemByFilter(PbSocialMasterFilter filter){
		List list = new ArrayList();
		try {
			list = ibatisEntityDao.getSqlMapClient().queryForList(
					getNameSpace() + ".getPbSocialItemByFilter", filter);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	public void insertMasterAndDetail(PbSocialMaster pbSocialMaster, List<PbSocialDetail> detailList){
		if (pbSocialMaster!=null) {
			try {
				ibatisEntityDao.getSqlMapClient().insert(getNameSpace()+".insertPbSocialMaster", pbSocialMaster);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(detailList!=null){
			for (PbSocialDetail detail : detailList) {
				try {
					ibatisEntityDao.getSqlMapClient().insert(getNameSpace()+".insertPbSocialDetail", detail);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void insertMasterAndDetail(PbSocialMaster pbSocialMaster){
		Long masterId = new Long(-1L);
		if (pbSocialMaster!=null) {
			try {
				masterId = (Long)ibatisEntityDao.getSqlMapClient().insert(getNameSpace()+".insertPbSocialMaster", pbSocialMaster);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		List<PbSocialDetail> detailList = pbSocialMaster.getDetailList();
		if(detailList!=null && masterId!=-1L){
			for (PbSocialDetail detail : detailList) {
				detail.setSocial_security_id(masterId);
				try {
					ibatisEntityDao.getSqlMapClient().insert(getNameSpace()+".insertPbSocialDetail", detail);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public PbSocialMaster getMasterAndDetailByMasterId(Long masterId){
		if (masterId == null) {
			return null;
		}
		
		PbSocialMaster entity = new PbSocialMaster();
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("masterId", masterId);
		List<PbSocialMaster> list = new ArrayList<PbSocialMaster>();
		try {
			list = ibatisEntityDao.getSqlMapClient()
					.queryForList(getNameSpace() + ".getPbSocialMasterByIdOrName",
							map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size() == 1) {
			entity = list.get(0);
			List<PbSocialDetail> detailList = new ArrayList<PbSocialDetail>();
			try {
				detailList = ibatisEntityDao.getSqlMapClient().queryForList(getNameSpace()+ ".getDetailListByMasterId", masterId);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			entity.setDetailList(detailList);
		}
		
		return entity;
	}

	public void insertPbSocialMaster(PbSocialMaster pbSocialMaster) {
		try {
			ibatisEntityDao.getSqlMapClient().insert(getNameSpace()+".insertPbSocialMaster", pbSocialMaster);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void insertPbSocialDetail(PbSocialDetail pbSocialDetail) {
		try {
			ibatisEntityDao.getSqlMapClient().insert(getNameSpace()+".insertPbSocialDetail", pbSocialDetail);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void deletePbSocialDetailById(Long id) {
		try {
			ibatisEntityDao.getSqlMapClient().delete(getNameSpace()+".deletePbSocialDetailById", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void deletePbSocialDetailByIds(String ids) {
		try {
			ibatisEntityDao.getSqlMapClient().delete(getNameSpace()+".deletePbSocialDetailByIds", ids);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void deletePbSocialMasterById(Long id) {
		try {
			ibatisEntityDao.getSqlMapClient().delete(getNameSpace()+".deletePbSocialMasterById", id);
			ibatisEntityDao.getSqlMapClient().delete(getNameSpace()+".deletePbSocialMasterByMasterId", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void deletePbSocialMasterByIds(String ids) {
		try {
			ibatisEntityDao.getSqlMapClient().delete(getNameSpace()+".deletePbSocialMasterByIds", ids);
			ibatisEntityDao.getSqlMapClient().delete(getNameSpace()+".deletePbSocialDetailByMasterIds", ids);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public PbSocialDetail getPbSocialDetailById(Long id) {
		if (id==null) {
			return null;
		}
		Map<String, Long> map =  new HashMap<String, Long>();
		map.put("masterId", id);
		List<PbSocialDetail> list = new ArrayList<PbSocialDetail>();
		try {
			list = ibatisEntityDao.getSqlMapClient().queryForList(getNameSpace()+".getPbSocialDetailById", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size()==1) {
			return list.get(0);
		}else
			return null;
	}

	public Page getPbSocialDetailPageByFilter(PbSocialDetailFilter filter) {
		return ibatisEntityDao.pagedQueryByFilter(this.getClass(), filter, 
				".getPbSocialDetailPageByFilter_count",
				".getPbSocialDetailPageByFilter_query");
	}

	public Page getPbSocialMasterPageByFilter(PbSocialMasterFilter filter) {
		return ibatisEntityDao.pagedQueryByFilter(this.getClass(), filter,
				".getPbSocialMasterPageByFilter_count",
				".getPbSocialMasterPageByFilter_query");
	}

	public PbSocialMaster getPbSocialMasterById(Long id) {
		if (id == null) {
			return null;
		}
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("masterId", id);
		List<PbSocialMaster> list = new ArrayList<PbSocialMaster>();
		try {
			list = ibatisEntityDao.getSqlMapClient()
					.queryForList(getNameSpace() + ".getPbSocialMasterByIdOrName",
							map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size() == 1) {
			return list.get(0);
		} else
			return null;
	}
	/**
	 * 根据社保名称或者id找到社保项目
	 * @param name 社保项目名称
	 * @return 社保项目
	 */
	public PbSocialMaster getPbSocialMasterByName(String name) {
		if (StringUtils.isEmpty(name)) {
			return null;
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("show_name", name);
		List<PbSocialMaster> list = new ArrayList<PbSocialMaster>();
		try {
			list = ibatisEntityDao.getSqlMapClient()
					.queryForList(getNameSpace() + ".getPbSocialMasterByIdOrName",
							map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size() == 1) {
			return list.get(0);
		} else
			return null;
	}
	
	/**
	 * 更新社保项目（策略：先删除再新增）
	 * @param pbSocialMaster
	 */
	public void fakeUpdateMasterAndDetail(PbSocialMaster pbSocialMaster) {
		if (pbSocialMaster == null)
			return;
		try {
			ibatisEntityDao.getSqlMapClient().update(
					getNameSpace() + ".updatePbSocialMaster", pbSocialMaster);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		List<PbSocialDetail> detailList = pbSocialMaster.getDetailList();
		if (detailList != null && detailList.size() > 0) {
			try {
				ibatisEntityDao.getSqlMapClient().delete(
						getNameSpace() + ".deletePbSocialDetailByMasterId",
						pbSocialMaster.getSocial_security_id());
			} catch (SQLException e) {
				e.printStackTrace();
			}
			for (PbSocialDetail detail : detailList) {
				detail.setSocial_security_id(pbSocialMaster.getSocial_security_id());
				insertPbSocialDetail(detail);
			}
		}
	}

	public void updatePbSocialDetail(PbSocialDetail pbSocialDetail) {
		try {
			ibatisEntityDao.getSqlMapClient().update(getNameSpace() + ".updatePbSocialDetail", pbSocialDetail);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void updatePbSocialMaster(PbSocialMaster pbSocialMaster) {
		if (pbSocialMaster==null) {
			return;
		}
		try {
			ibatisEntityDao.getSqlMapClient().update(getNameSpace() + ".updatePbSocialMaster", pbSocialMaster);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//还需讨论如何处理
		List<PbSocialDetail> addDs = pbSocialMaster.getAddList();
		List<PbSocialDetail> deleteDs = pbSocialMaster.getDelList();
		List<PbSocialDetail> modifieDs = pbSocialMaster.getUpdList();
		//处理新增的
		for (PbSocialDetail detail : addDs) {
			try {
				ibatisEntityDao.getSqlMapClient().insert(getNameSpace() + ".insertPbSocialDetail", detail);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//处理删除的
		if(deleteDs.size()>0) {
			StringBuffer sb = new StringBuffer();
			for (PbSocialDetail detail : deleteDs) {
				sb.append(detail.getSub_social_security_id()).append(",");
			}
			
			try {
				ibatisEntityDao.getSqlMapClient().delete(getNameSpace() + ".deletePbSocialDetailByIds", sb.substring(0, sb.length()-1));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//处理更新的
		for (PbSocialDetail detail : modifieDs) {
			try {
				ibatisEntityDao.getSqlMapClient().update(getNameSpace() + ".updatePbSocialDetail", detail);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public List<RecomTemplateSocialItemSearchBean> getSocialItemByCompanCode(String companyCode) throws SQLException{
		Map<String, String> map = new HashMap<String, String>();
		map.put("companyCode", companyCode);
		map.put("empStr", null);
		return ibatisEntityDao.getSqlMapClient().queryForList(getNameSpace() + ".getSocialItemByCompanCode", map);
	}
	public List<RecomTemplateSocialItemSearchBean> getSocialItemByEmps(String companyCode, String empIds) throws SQLException{
		if (StringUtils.isEmpty(companyCode) || StringUtils.isEmpty(empIds)) {
		 	return null;
		}
		String[] str = empIds.split(",");
		StringBuffer sb = new StringBuffer();
		for (String string : str) {
			sb.append("select distinct ce.social_security_id").append(
					" from cs_emplsocial ce, pb_employee pe").append(
					" where ce.employee_id = pe.employee_id").append(
					" and ce.is_valid = 1").append(
					" and ce.employee_id = " + string + "").append(
					" and ce.company_code = '" + companyCode + "' ").append(appendString);
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("companyCode", companyCode);
		map.put("empStr", sb.substring(0, sb.length()-appendString.length()));
		return ibatisEntityDao.getSqlMapClient().queryForList(getNameSpace() + ".getSocialItemByCompanCode", map);
	}
}
